<?php

namespace App\Models\Booked;

use App\Models\BaseModel;

class Booked extends BaseModel
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id',
        'shop_id',
        'manager_user_id',
        'status',
        'mobile',
        'num',
        'time',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [];

    protected $table = 'booked';
    protected $dateFormat = 'U';

    /**
     * 用户预定列表
     * @param $page
     * @param $size
     * @param $user_id
     * @return mixed
     */
    public function user_booked($page, $size, $user_id){
        $data = [];
        $data['list'] = $this->where('user_id', $user_id)
            ->withOnly('manager', ['user_id', 'mobile'])
            ->withOnly('shop', ['shop_id', 'title'])
            ->forPage($page, $size+1)
            ->orderBy('updated_at', 'desc')
            ->get()
            ->toArray();
        $data['next_page'] = $page + 1;
        $data['page_size'] = $size;
        return $this->after($data);
    }

    /**
     * 经理预定列表
     * @param $page
     * @param $size
     * @param $manager_user_id
     * @param $shop_id
     * @return mixed
     */
    public function manager_booked($page, $size, $manager_user_id, $shop_id){
        $data = [];
        $data['list'] = $this->where('manager_user_id', $manager_user_id)
            ->where('shop_id', $shop_id)
            ->withOnly('user', ['user_id', 'mobile', 'nickname', 'cover'])
            ->forPage($page, $size+1)
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();
        $data['next_page'] = $page + 1;
        $data['page_size'] = $size;
        return $this->after($data);
    }

    /**
     * 关联经理
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function manager(){
        return $this->hasOne('App\Models\User\User', 'user_id', 'manager_user_id');
    }

    /**
     * 关联商家
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function shop(){
        return $this->hasOne('App\Models\Shop\Shop', 'shop_id', 'shop_id');
    }

    /**
     * 关联用户
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function user(){
        return $this->hasOne('App\Models\User\User', 'user_id', 'user_id');
    }
}
